package com.todo.strategy.offline.application.handler.statusChange;

import com.alibaba.fastjson.JSONObject;
import com.todo.biz.common.enums.DetectionRiskLevelEnum;
import com.todo.biz.service.api.request.ContentDetectionRequest;
import com.todo.biz.service.api.response.BaseExamineResponse;
import com.todo.common.common.entity.response.BizResponse;
import com.todo.common.common.enums.BusinessSceneEnum;
import com.todo.strategy.common.enums.StrategyStatusEnum;
import com.todo.strategy.offline.application.entity.bo.PlatformStrategyDetailBO;
import com.todo.strategy.offline.application.entity.bo.PlatformStrategyExtendBO;
import com.todo.strategy.offline.application.entity.context.StrategyStatusContext;
import com.todo.strategy.offline.application.remote.BizExamineRemoteService;
import com.todo.strategy.offline.application.service.PlatformStrategyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author azhebuxing
 * @date 2024/12/12 19:52
 * @description
 */
@Slf4j
@Component
public class PostHandler extends AbstractStrategyStatusHandler {

    @Resource
    private PlatformStrategyService platformStrategyService;
    @Resource
    private BizExamineRemoteService bizExamineRemoteService;

    @Override
    protected Boolean doStatusHandle(StrategyStatusContext context) {
        Long strategyId = context.getStrategyId();
        PlatformStrategyDetailBO strategyInfoBO = platformStrategyService.queryPlatformStrategyDetailById(strategyId);
        if (strategyInfoBO == null || strategyInfoBO.getPlatformStrategyBO() == null) {
            log.error("PostHandler#doStatusHandle 平台攻略发布状态处理完成，数据为空，默认执行成功 context:{}", JSONObject.toJSONString(context));
            return true;
        }
        if (!strategyInfoBO.getPlatformStrategyBO().getStatus().equals(context.getCurrentStatus())) {
            log.error("PostHandler#doStatusHandle 平台攻略当前数据状态与发送消息状态不一致，无法处理当前状态下的数据情况 context:{}，当前状态 :{}", JSONObject.toJSONString(context), strategyInfoBO.getPlatformStrategyBO().getStatus());
            return true;
        }
        PlatformStrategyExtendBO platformStrategyExtendBO = strategyInfoBO.getPlatformStrategyExtendBO();
        StringBuilder sb = new StringBuilder();
        if (platformStrategyExtendBO.getInstructions() != null) {
            sb.append(platformStrategyExtendBO.getInstructions().getLocalCulture());
        }
        if (platformStrategyExtendBO.getTravelSuggest() != null) {
            sb.append(platformStrategyExtendBO.getTravelSuggest().getTravelSuggest());
            sb.append(platformStrategyExtendBO.getTravelSuggest().getSeasonSuggest());
            sb.append(platformStrategyExtendBO.getTravelSuggest().getDisclose());
        }
        sb.append(platformStrategyExtendBO.getAgeSuggest());
        ContentDetectionRequest request = new ContentDetectionRequest();
        request.setContent(sb.toString());
        request.setBizId(strategyId);
        request.setSceneEnum(BusinessSceneEnum.STRATEGY);
        BizResponse<BaseExamineResponse> bizResponse = bizExamineRemoteService.contentDetection(request);
        if (bizResponse == null || !bizResponse.isSuccess() || bizResponse.getData() == null) {
            //默认审核数据失败情况，转人工
            Boolean updated = platformStrategyService.updatePlatformStrategyStatus(strategyId, StrategyStatusEnum.AUTO_EXAMINE_FAILED);
            return updated;
        }
        BaseExamineResponse baseExamineResponse = bizResponse.getData();
        if (baseExamineResponse.getReviewLevelEnum().equals(DetectionRiskLevelEnum.MEDIUM)) {
            //审核数据为中等，建议
            Boolean updated = platformStrategyService.updatePlatformStrategyStatus(strategyId, StrategyStatusEnum.AUTO_EXAMINE_FAILED);
            return updated;
        }
        if (baseExamineResponse.getReviewLevelEnum().equals(DetectionRiskLevelEnum.HIGH)) {
            log.warn("PostHandler#doStatusHandle 平台攻略发布状态处理完成，数据为高风险，默认执行失败 结果:{}", JSONObject.toJSONString(baseExamineResponse));
            Boolean updated = platformStrategyService.updatePlatformStrategyStatus(strategyId, StrategyStatusEnum.DISCARD);
            //TODO 异常数据预警
            return updated;
        }
        Boolean updated = platformStrategyService.updatePlatformStrategyStatus(strategyId, StrategyStatusEnum.PUBLISHED);
        return updated;
    }

    @Override
    protected boolean match(StrategyStatusContext context) {
        if (context == null || context.getStrategyId() == null || context.getCurrentStatus() == null) {
            log.error("PostHandler#match 平台攻略前置数据匹配失败，数据缺失或不存在 context:{}", JSONObject.toJSONString(context));
            return false;
        }
        return true;
    }

    @Override
    protected StrategyStatusEnum doGetDealStatus() {
        return StrategyStatusEnum.POSTED;
    }
}
